package com.ktgj.customer.service.impl;

import java.util.*;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.domain.dto.DepartmentTreeSelectDTO;
import com.ktgj.customer.domain.vo.DepartmentsVo;
import com.ktgj.customer.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.DepartmentMapper;
import com.ktgj.customer.domain.Department;
import com.ktgj.customer.service.IDepartmentService;

/**
 * 部门Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询部门
     *
     * @param deptId 部门ID
     * @return 部门
     */
    @Override
    public Department selectDepartmentById(Long deptId) {
        return departmentMapper.selectDepartmentById(deptId);
    }

    /**
     * 查询部门列表
     *
     * @param department 部门
     * @return 部门
     */
    @Override
    public List<Department> selectDepartmentList(Department department) {
        return departmentMapper.selectDepartmentList(department);
    }

    /**
     * 新增部门
     *
     * @param department 部门
     * @return 结果
     */
    @Override
    public int insertDepartment(Department department) {
        department.setCreateTime(new Date());
        department.setStatus(CustomerConstants.normal.getValue());
        return departmentMapper.insertDepartment(department);
    }

    /**
     * 修改部门
     *
     * @param department 部门
     * @return 结果
     */
    @Override
    public int updateDepartment(Department department) {
        return departmentMapper.updateDepartment(department);
    }

    /**
     * 批量删除部门
     *
     * @param deptIds 需要删除的部门ID
     * @return 结果
     */
    @Override
    public int deleteDepartmentByIds(Long[] deptIds) {
        return departmentMapper.deleteDepartmentByIds(deptIds);
    }

    /**
     * 删除部门信息
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Override
    public int deleteDepartmentById(Long deptId) {
        return departmentMapper.deleteDepartmentById(deptId);
    }

    /**
     * 查询出所有用树桩显示
     *
     * @return
     */
    @Override
    public List<Department> departmentList(Department department) {
        List<Department> list = departmentMapper.selectDepartmentList(department);
        if (list == null)
            return null;

        List<Department> departmentList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getParentDeptId().equals(0L) || list.get(i).getParentDeptId() == null) {
                departmentList.add(list.get(i));
                list.remove(i);
                i--;
            }
        }

        if (departmentList.size() < 1)
            return list;

        for (Department item : departmentList) {
            item.setDepartmentList(this.departmentListByParentId(list, item.getDeptId()));
        }

        return departmentList;
    }

    public List<Department> departmentListByParentId(List<Department> list, Long parentId) {
        List<Department> departmentList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getParentDeptId().equals(parentId)) {
                departmentList.add(list.get(i));
                list.remove(i);
                i--;
            }
        }

        if (departmentList.size() != 0)//如果.size()==0的话，说明所有数据中，没有父id和传进来的id是相同的，也就是说没有子级了
            for (Department item : departmentList) {
                item.setDepartmentList(this.departmentListByParentId(list, item.getDeptId()));
            }
        return departmentList;
    }

    public List<DepartmentTreeSelectDTO> departmentAllByParentId(List<DepartmentTreeSelectDTO> list, Long parentId) {
        List<DepartmentTreeSelectDTO> departmentList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getParentDeptId().equals(parentId)) {
                departmentList.add(list.get(i));
                list.remove(i);
                i--;
            }
        }

        if (departmentList.size() != 0)//如果.size()==0的话，说明所有数据中，没有父id和传进来的id是相同的，也就是说没有子级了
            for (DepartmentTreeSelectDTO item : departmentList) {
                item.setList(this.departmentAllByParentId(list, item.getDeptId()));
            }
        return departmentList;
    }

    @Override
    public Integer queryEmployeeCountByDeptId(Long deptId) {
        return departmentMapper.queryEmployeeCountByDeptId(deptId);
    }

    @Override
    public int deactivate(Long deptId) {
        Department department = new Department();
        department.setStatus(CustomerConstants.deactivate.getValue());
        department.setDeptId(deptId);
        return departmentMapper.updateDepartment(department);
    }

    @Override
    public int enable(Long deptId) {
        Department department = new Department();
        department.setStatus(CustomerConstants.normal.getValue());
        department.setDeptId(deptId);
        return departmentMapper.updateDepartment(department);
    }

    @Override
    public List<DepartmentTreeSelectDTO> getAll() {
        List<DepartmentTreeSelectDTO> list = departmentMapper.getAll();
        if (list == null)
            return null;

        List<DepartmentTreeSelectDTO> departmentList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getParentDeptId().equals(0L) || list.get(i).getParentDeptId() == null) {
                departmentList.add(list.get(i));
                list.remove(i);
                i--;
            }
        }

        if (departmentList.size() < 1)
            return list;

        for (DepartmentTreeSelectDTO item : departmentList) {
            item.setList(this.departmentAllByParentId(list, item.getDeptId()));
        }

        return departmentList;
    }

    /**
     * 查询员工所在部门（远程）
     * */
    @Override
    public Department department(Long id) {
        Employee employee = employeeMapper.backSelectEmployee(id);
        if (employee != null){
            Department department = departmentMapper.selectDepartment(employee.getDeptId());
            if (department != null){
                return department;
            }
        }
        return null;
    }

    /**
     * id获取部门名称（远程）
     * */
    @Override
    public Department deptName(Long id) {
        Department department = departmentMapper.selectDepartment(id);
        return department;
    }

    /**
     * 部门列表
     * */
    @Override
    public List<Department> departments() {
        List<Department> departmentList = departmentMapper.departments();
        return departmentList;
    }
}